############################################################
#Demograhpics (Sans Values)
#Figures OB5 & OB6
############################################################

library(rio)
library(tidyverse)
library(ggstance)

###########################################
#Values from Model 3
###########################################

#####################
#Functions
####################

`%notin%` <- Negate(`%in%`)

v_func <- function(x) {
  x %>%
    filter(parm %in% c("self_te", "cons_open")) %>%
    mutate(model = "Model 3 (In-Text)", 
           values = "Values")
}


v3 <- import("vote_elections3.dta") %>% v_func()
s3 <- import("support_others3.dta") %>% v_func()
o3 <- import("obey3.dta") %>% v_func()
op3 <- import("opinions3.dta") %>% v_func()
vo3 <- import("volunteer3.dta") %>% v_func()
a3 <- import("beactive3.dta") %>% v_func()



###########################################
#Demographics
###########################################

d_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01", "cons_open", "self_te")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 2 (In-Text)", 
           values = "Demographics")
    
}

d_func1 <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "No Values", 
           values = "Demographics")
  
}


v_dem1 <- import("vote_elections2.dta") %>% d_func()
v_dem2 <- import("vote_elections2_justdem.dta") %>% d_func1()


s_dem1 <- import("support_others2.dta") %>% d_func()
s_dem2 <- import("support_others2_justdem.dta") %>% d_func1()

vol_dem1 <- import("volunteer2.dta") %>% d_func()
vol_dem2 <- import("volunteer2_justdem.dta") %>% d_func1()

ob_dem1 <- import("obey2.dta") %>% d_func()
ob_dem2 <- import("obey2_justdem.dta") %>% d_func1()

op_dem1 <- import("opinions2.dta") %>% d_func()
op_dem2 <- import("opinions2_justdem.dta") %>% d_func1()

be_dem1 <- import("beactive2.dta") %>% d_func()
be_dem2 <- import("beactive2_justdem.dta") %>% d_func1()

############################
# Combine
############################

comb <- bind_rows(v3, s3, o3, op3, vo3, a3, 
                  v_dem1, v_dem2, 
                  s_dem1, s_dem2, 
                  vol_dem1, vol_dem2, 
                  ob_dem1, ob_dem2, 
                  op_dem1, op_dem2, 
                  be_dem1, be_dem2)


comb1a <- comb %>%
  filter(parm != "0.edulvlfa") %>%
  mutate(parm = factor(parm, 
                       levels=c("self_te", "cons_open", 
                                "age", "1.gender", 
                                "1.minority1", "1.union1", 
                                "2.edulvla", "3.edulvla", "4.edulvla", "5.edulvla", 
                                "2.edulvlfa", "3.edulvlfa", "4.edulvlfa", "5.edulvlfa", 
                                "2.born_when1", "3.born_when1"), 
                       labels=c("Self Transcendence", "Conservation", 
                                "Age", "Female", 
                                "Minority", "Union Mmbr.", 
                                "Lower Secondary (Self)", "Secondary (Self)", "Post-Secondary (Self)", 
                                "Tertiary Ed (Self)", 
                                "Lower Secondary (Father)", "Secondary (Father)", "Post-Secondary (Father)", 
                                "Tertiary Ed (Father)", 
                                "Came to Cntry <20yrs Ago", "Came to Cntry 20+yrs Ago")), 
         eq = factor(eq, 
                     levels=c("vote_elections", "beactive", "volunteer", 
                              "obey", "opinions", "support_others"), 
                     labels=c("Vote", "Be Active", "Volunteer", 
                              "Obey", "Opinions", "Support Others")), 
         model = factor(model, 
                        levels=c("Model 3 (In-Text)", 
                                 "Model 2 (In-Text)", 
                                 "No Values")))
         
         
partic <- comb1a %>% filter(eq %in% c("Vote", "Be Active", "Volunteer"))

ggplot(partic, aes(x=estimate, y=parm, shape=model)) + 
  geom_pointrangeh(aes(xmin=min95, xmax=max95), 
                   position = position_dodgev(height=0.3)) + 
  facet_grid(values ~ eq, labeller = label_wrap_gen(width = 25), scales="free", space="free") + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  labs(y=NULL, x = NULL) + 
  theme_bw(14) + 
  theme(strip.text.y = element_text(angle=0), 
        axis.title.y = element_text(margin = margin(r=15)), 
        strip.text = element_text(face = "bold"), 
        legend.position = "top") 

ggsave("figure_ob5.png", height=12, width=10, dpi=600)

nonpartic <- comb1a %>% filter(eq %notin% c("Vote", "Be Active", "Volunteer"))

ggplot(nonpartic, aes(x=estimate, y=parm, shape=model)) + 
  geom_pointrangeh(aes(xmin=min95, xmax=max95), 
                   position = position_dodgev(height=0.3)) + 
  facet_grid(values ~ eq, labeller = label_wrap_gen(width = 25), scales="free", space="free") + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  labs(y=NULL, x = NULL) + 
  theme_bw(14) + 
  theme(strip.text.y = element_text(angle=0), 
        axis.title.y = element_text(margin = margin(r=15)), 
        strip.text = element_text(face = "bold"), 
        legend.position = "top") 

ggsave("figure_ob6.png", height=12, width=10, dpi=600)
